#include "plugin.h"

#include <string.h>
#include <GL\glew.h>

// forward declarations
void CreateDefaultBrush(float** textureData, int* width, int* height);

// ----------------------------------------------------------------------------
TextureInfo* GetTextureInfo(BWInstanceData* instance, const char* filename)
{
    int i;
    TextureInfo* textureInfo;

    if (strlen(filename) == 0) filename = DEFAULT_BRUSH;

    for (i=0; i<MAX_NUM_TEXTURES; ++i)
    {
        textureInfo = &instance->textureInfos[i];

        // new texture
        if (strlen(textureInfo->filename) == 0)
            break; 

        // texture already loaded
        if (strcmp(filename, textureInfo->filename) == 0)
            return textureInfo;
    }

    if (i == MAX_NUM_TEXTURES)
    {
        char msg[] = "Too many brush textures used!";        
        WriteTrace(msg);        
        showLWMessage(instance->globalFunc, LWMsgType_Warning,
                msg, "Skipping object.");        
        return NULL;
    }

    // set default values for now.
    // texture is loaded directly before it is used during rendering,
    // to minimize the chance that Lightwave destroys it in the meantime
    // by issuing 'evil' OpenGL commands.
    
    strcpy_s(textureInfo->filename, MAX_FILENAME_LENGTH, filename);
    textureInfo->width = -1;
    textureInfo->height = -1;
    textureInfo->glTextureID = 0;
    textureInfo->lwImageID = NULL;
    textureInfo->isLoaded = FALSE;    

    return textureInfo;
}

void LoadTexture(BWInstanceData* instance, TextureInfo* textureInfo)
{
    int x, y, pos;
    const LWImageList* lwImageList;
    LWImageID lwImageID;
    LWBufferValue rgb[3];
    LWBufferValue alpha;  
    float* textureData;

    WriteTrace("Loading texture '%s'", textureInfo->filename);

    // texture not yet loaded -- do it now.    
    if (strcmp(textureInfo->filename, DEFAULT_BRUSH) == 0)
    {
        lwImageID = NULL;
        CreateDefaultBrush(&textureData, &textureInfo->width, 
            &textureInfo->height);
    }
    else
    {           
        lwImageList = getLWImageList(instance->globalFunc);
        lwImageID = lwImageList->load(textureInfo->filename);

        if (!lwImageID)
        {
            char errorMsg[300];
            sprintf_s(errorMsg, 300, "Could not open texture: %s", 
                textureInfo->filename);

            showLWMessage(instance->globalFunc, LWMsgType_Error, 
                errorMsg, "Using default texture instead.");

            CreateDefaultBrush(&textureData, &textureInfo->width, 
                &textureInfo->height);
        }
        else
        {
            lwImageList->size(lwImageID, &textureInfo->width, 
                &textureInfo->height);

            textureData = calloc(textureInfo->width * textureInfo->height * 4, 
                sizeof(float));

            for (y=0; y<textureInfo->height; ++y)
            {
                for (x=0; x<textureInfo->width; ++x)
                {
                    pos = (y * textureInfo->width + x) * 4;
                    lwImageList->RGB(lwImageID, x, y, rgb);
                    
                    if (lwImageList->hasAlpha(lwImageID))
                        alpha = lwImageList->alpha(lwImageID, x, y);
                    else
                        alpha = 1.0f;

                    textureData[pos  ] = rgb[0];
                    textureData[pos+1] = rgb[1];
                    textureData[pos+2] = rgb[2];
                    textureData[pos+3] = alpha;
                }
            }
        }
    }

    textureInfo->lwImageID = lwImageID;
    
    // create OpenGL texture
    
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glGenTextures(1, &textureInfo->glTextureID);
    
    glBindTexture(GL_TEXTURE_2D, textureInfo->glTextureID);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    // I have tried GL_LINEAR_MIPMAP_NEAREST, but it makes some brushes unusually dark

    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, 
        textureInfo->width, textureInfo->height, 
        GL_RGBA, GL_FLOAT, textureData);

    // this function was replaced by the above, since it yields in a better image quality:
    //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 
    //    textureInfo->width, textureInfo->height, 
    //    0, GL_RGBA, GL_FLOAT, textureData); 
   
    glBindTexture(GL_TEXTURE_2D, 0);

    textureInfo->isLoaded = TRUE;

    free(textureData); // no longer needed - OpenGL already has a copy
}

void ReleaseTextures(BWInstanceData* instance)
{
    int i;
    const LWImageList* lwImageList = getLWImageList(instance->globalFunc);
    TextureInfo* textureInfo;

    for (i=0; i<MAX_NUM_TEXTURES; ++i)
    {
        textureInfo = &instance->textureInfos[i];
        if (strlen(textureInfo->filename) != 0)
        {
            lwImageList->clear(textureInfo->lwImageID);
            glDeleteTextures(1, &textureInfo->glTextureID);
            
            strcpy_s(textureInfo->filename, MAX_FILENAME_LENGTH, "");
            textureInfo->glTextureID = 0;
            textureInfo->lwImageID = NULL;
        }
    }
}

void CreateDefaultBrush(float** textureData, int* width, int* height)
{
    int i;
    float data[] = {
        1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0.007843138f, 1,1,1,0.02745098f, 1,1,1,0.05490196f, 1,1,1,0.1019608f, 1,1,1,0.1843137f, 1,1,1,0.1137255f, 1,1,1,0.01960784f, 1,1,1,0.03921569f, 1,1,1,0.01960784f, 1,1,1,0.007843138f, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 
        1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0.007843138f, 1,1,1,0.07843138f, 1,1,1,0.2784314f, 1,1,1,0.4823529f, 1,1,1,0.5843138f, 1,1,1,0.4862745f, 1,1,1,0.1686275f, 1,1,1,0.1019608f, 1,1,1,0.1490196f, 1,1,1,0.03921569f, 1,1,1,0.02745098f, 1,1,1,0.01960784f, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0.007843138f, 1,1,1,0.007843138f, 1,1,1,0.01568628f, 1,1,1,0.003921569f, 1,1,1,0.003921569f, 1,1,1,0.003921569f, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 
        1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0.08627451f, 1,1,1,0.3843137f, 1,1,1,0.6313726f, 1,1,1,0.7137255f, 1,1,1,0.7137255f, 1,1,1,0.3686275f, 1,1,1,0.145098f, 1,1,1,0.2627451f, 1,1,1,0.1372549f, 1,1,1,0.2039216f, 1,1,1,0.2235294f, 1,1,1,0.2196078f, 1,1,1,0.1607843f, 1,1,1,0.05882353f, 1,1,1,0, 1,1,1,0.003921569f, 1,1,1,0.02352941f, 1,1,1,0.2078431f, 1,1,1,0.1294118f, 1,1,1,0.06666667f, 1,1,1,0.03529412f, 1,1,1,0, 1,1,1,0.003921569f, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 
        1,1,1,0, 1,1,1,0, 1,1,1,0.003921569f, 1,1,1,0.02352941f, 1,1,1,0.02352941f, 1,1,1,0.2f, 1,1,1,0.6509804f, 1,1,1,0.7333333f, 1,1,1,0.7568628f, 1,1,1,0.7450981f, 1,1,1,0.6627451f, 1,1,1,0.5686275f, 1,1,1,0.4666667f, 1,1,1,0.2039216f, 1,1,1,0.3843137f, 1,1,1,0.427451f, 1,1,1,0.6156863f, 1,1,1,0.7529412f, 1,1,1,0.2862745f, 1,1,1,0.4705882f, 1,1,1,0.2235294f, 1,1,1,0.07058824f, 1,1,1,0.3529412f, 1,1,1,0.1921569f, 1,1,1,0.07450981f, 1,1,1,0.08627451f, 1,1,1,0.07843138f, 1,1,1,0.03137255f, 1,1,1,0.01568628f, 1,1,1,0, 1,1,1,0, 1,1,1,0, 
        1,1,1,0, 1,1,1,0, 1,1,1,0.007843138f, 1,1,1,0.1019608f, 1,1,1,0.1137255f, 1,1,1,0.3490196f, 1,1,1,0.7490196f, 1,1,1,0.7294118f, 1,1,1,0.4705882f, 1,1,1,0.8078431f, 1,1,1,0.6745098f, 1,1,1,0.6941177f, 1,1,1,0.5568628f, 1,1,1,0.2784314f, 1,1,1,0.3568628f, 1,1,1,0.6156863f, 1,1,1,0.8039216f, 1,1,1,0.9254902f, 1,1,1,0.8313726f, 1,1,1,0.7803922f, 1,1,1,0.454902f, 1,1,1,0.227451f, 1,1,1,0.3372549f, 1,1,1,0.1843137f, 1,1,1,0.0627451f, 1,1,1,0.1764706f, 1,1,1,0.2705882f, 1,1,1,0.1058824f, 1,1,1,0.01960784f, 1,1,1,0.003921569f, 1,1,1,0, 1,1,1,0, 
        1,1,1,0, 1,1,1,0, 1,1,1,0.003921569f, 1,1,1,0.07843138f, 1,1,1,0.2352941f, 1,1,1,0.4392157f, 1,1,1,0.5254902f, 1,1,1,0.5686275f, 1,1,1,0.5137255f, 1,1,1,0.654902f, 1,1,1,0.6666667f, 1,1,1,0.427451f, 1,1,1,0.3254902f, 1,1,1,0.2705882f, 1,1,1,0.3607843f, 1,1,1,0.7647059f, 1,1,1,0.9098039f, 1,1,1,0.9294118f, 1,1,1,0.827451f, 1,1,1,0.6941177f, 1,1,1,0.7450981f, 1,1,1,0.5960785f, 1,1,1,0.4352941f, 1,1,1,0.2509804f, 1,1,1,0.09411765f, 1,1,1,0.227451f, 1,1,1,0.4352941f, 1,1,1,0.3686275f, 1,1,1,0.08627451f, 1,1,1,0.02745098f, 1,1,1,0, 1,1,1,0, 
        1,1,1,0, 1,1,1,0.007843138f, 1,1,1,0.03529412f, 1,1,1,0.1529412f, 1,1,1,0.372549f, 1,1,1,0.3176471f, 1,1,1,0.1921569f, 1,1,1,0.4039216f, 1,1,1,0.4705882f, 1,1,1,0.3098039f, 1,1,1,0.3294118f, 1,1,1,0.2117647f, 1,1,1,0.1686275f, 1,1,1,0.1411765f, 1,1,1,0.227451f, 1,1,1,0.7686275f, 1,1,1,0.945098f, 1,1,1,0.8235294f, 1,1,1,0.6745098f, 1,1,1,0.7921569f, 1,1,1,0.8705882f, 1,1,1,0.8666667f, 1,1,1,0.5490196f, 1,1,1,0.5215687f, 1,1,1,0.3058824f, 1,1,1,0.227451f, 1,1,1,0.5647059f, 1,1,1,0.4901961f, 1,1,1,0.1490196f, 1,1,1,0.05098039f, 1,1,1,0.003921569f, 1,1,1,0, 
        1,1,1,0, 1,1,1,0.03529412f, 1,1,1,0.1058824f, 1,1,1,0.09803922f, 1,1,1,0.1333333f, 1,1,1,0.1137255f, 1,1,1,0.07058824f, 1,1,1,0.3647059f, 1,1,1,0.7215686f, 1,1,1,0.3882353f, 1,1,1,0.1019608f, 1,1,1,0.1764706f, 1,1,1,0.2509804f, 1,1,1,0.2117647f, 1,1,1,0.1529412f, 1,1,1,0.6235294f, 1,1,1,0.8862745f, 1,1,1,0.5058824f, 1,1,1,0.6196079f, 1,1,1,0.8627451f, 1,1,1,0.7215686f, 1,1,1,0.8470588f, 1,1,1,0.7411765f, 1,1,1,0.6941177f, 1,1,1,0.4823529f, 1,1,1,0.6431373f, 1,1,1,0.8156863f, 1,1,1,0.4156863f, 1,1,1,0.08235294f, 1,1,1,0.05882353f, 1,1,1,0.03529412f, 1,1,1,0, 
        1,1,1,0, 1,1,1,0.07843138f, 1,1,1,0.2901961f, 1,1,1,0.5529412f, 1,1,1,0.5058824f, 1,1,1,0.4627451f, 1,1,1,0.5333334f, 1,1,1,0.4823529f, 1,1,1,0.5254902f, 1,1,1,0.4156863f, 1,1,1,0.372549f, 1,1,1,0.4156863f, 1,1,1,0.509804f, 1,1,1,0.454902f, 1,1,1,0.345098f, 1,1,1,0.5647059f, 1,1,1,0.7803922f, 1,1,1,0.6313726f, 1,1,1,0.7411765f, 1,1,1,0.7450981f, 1,1,1,0.4313726f, 1,1,1,0.8196079f, 1,1,1,0.8431373f, 1,1,1,0.8470588f, 1,1,1,0.7137255f, 1,1,1,0.6470588f, 1,1,1,0.7098039f, 1,1,1,0.3019608f, 1,1,1,0.1372549f, 1,1,1,0.1372549f, 1,1,1,0.05098039f, 1,1,1,0, 
        1,1,1,0, 1,1,1,0.05098039f, 1,1,1,0.2745098f, 1,1,1,0.6156863f, 1,1,1,0.8f, 1,1,1,0.7529412f, 1,1,1,0.8313726f, 1,1,1,0.8784314f, 1,1,1,0.6666667f, 1,1,1,0.6470588f, 1,1,1,0.6078432f, 1,1,1,0.654902f, 1,1,1,0.6941177f, 1,1,1,0.7176471f, 1,1,1,0.7450981f, 1,1,1,0.7176471f, 1,1,1,0.5490196f, 1,1,1,0.4862745f, 1,1,1,0.6f, 1,1,1,0.3882353f, 1,1,1,0.5764706f, 1,1,1,0.8588235f, 1,1,1,0.8980392f, 1,1,1,0.7333333f, 1,1,1,0.5333334f, 1,1,1,0.5372549f, 1,1,1,0.682353f, 1,1,1,0.3764706f, 1,1,1,0.282353f, 1,1,1,0.2235294f, 1,1,1,0.07450981f, 1,1,1,0.003921569f, 
        1,1,1,0, 1,1,1,0, 1,1,1,0.09411765f, 1,1,1,0.1137255f, 1,1,1,0.6745098f, 1,1,1,0.7098039f, 1,1,1,0.772549f, 1,1,1,0.8588235f, 1,1,1,0.8431373f, 1,1,1,0.8666667f, 1,1,1,0.7411765f, 1,1,1,0.7647059f, 1,1,1,0.8156863f, 1,1,1,0.8313726f, 1,1,1,0.8039216f, 1,1,1,0.8627451f, 1,1,1,0.7568628f, 1,1,1,0.5686275f, 1,1,1,0.5058824f, 1,1,1,0.2627451f, 1,1,1,0.3411765f, 1,1,1,0.5490196f, 1,1,1,0.682353f, 1,1,1,0.4823529f, 1,1,1,0.227451f, 1,1,1,0.7294118f, 1,1,1,0.8078431f, 1,1,1,0.3647059f, 1,1,1,0.3882353f, 1,1,1,0.3960784f, 1,1,1,0.1254902f, 1,1,1,0.02745098f, 
        1,1,1,0, 1,1,1,0.003921569f, 1,1,1,0.2313726f, 1,1,1,0.5607843f, 1,1,1,0.4039216f, 1,1,1,0.772549f, 1,1,1,0.7882353f, 1,1,1,0.8666667f, 1,1,1,0.7843137f, 1,1,1,0.772549f, 1,1,1,0.8039216f, 1,1,1,0.7960784f, 1,1,1,0.8470588f, 1,1,1,0.8705882f, 1,1,1,0.8980392f, 1,1,1,0.9294118f, 1,1,1,0.9058824f, 1,1,1,0.7137255f, 1,1,1,0.7686275f, 1,1,1,0.8509804f, 1,1,1,0.682353f, 1,1,1,0.4039216f, 1,1,1,0.7411765f, 1,1,1,0.6156863f, 1,1,1,0.3960784f, 1,1,1,0.6039216f, 1,1,1,0.5372549f, 1,1,1,0.3607843f, 1,1,1,0.6196079f, 1,1,1,0.6196079f, 1,1,1,0.2745098f, 1,1,1,0.09411765f, 
        1,1,1,0, 1,1,1,0.02352941f, 1,1,1,0.1254902f, 1,1,1,0.6078432f, 1,1,1,0.4431373f, 1,1,1,0.8156863f, 1,1,1,0.9058824f, 1,1,1,0.9058824f, 1,1,1,0.8588235f, 1,1,1,0.8f, 1,1,1,0.682353f, 1,1,1,0.8313726f, 1,1,1,0.9098039f, 1,1,1,0.9215686f, 1,1,1,0.9372549f, 1,1,1,0.9215686f, 1,1,1,0.8784314f, 1,1,1,0.7882353f, 1,1,1,0.8980392f, 1,1,1,0.9411765f, 1,1,1,0.8431373f, 1,1,1,0.7490196f, 1,1,1,0.6588235f, 1,1,1,0.7568628f, 1,1,1,0.1372549f, 1,1,1,0.2235294f, 1,1,1,0.2156863f, 1,1,1,0.2039216f, 1,1,1,0.4980392f, 1,1,1,0.4431373f, 1,1,1,0.2078431f, 1,1,1,0.07450981f, 
        1,1,1,0, 1,1,1,0.02352941f, 1,1,1,0.1098039f, 1,1,1,0.2627451f, 1,1,1,0.6588235f, 1,1,1,0.8941177f, 1,1,1,0.8666667f, 1,1,1,0.8627451f, 1,1,1,0.9607843f, 1,1,1,0.9058824f, 1,1,1,0.7411765f, 1,1,1,0.8470588f, 1,1,1,0.9490196f, 1,1,1,0.9137255f, 1,1,1,0.9372549f, 1,1,1,0.9294118f, 1,1,1,0.9372549f, 1,1,1,0.9411765f, 1,1,1,0.9411765f, 1,1,1,0.9098039f, 1,1,1,0.7764706f, 1,1,1,0.7254902f, 1,1,1,0.6980392f, 1,1,1,0.7450981f, 1,1,1,0.2666667f, 1,1,1,0.07450981f, 1,1,1,0.1019608f, 1,1,1,0.1294118f, 1,1,1,0.2980392f, 1,1,1,0.1607843f, 1,1,1,0.04313726f, 1,1,1,0.02352941f, 
        1,1,1,0.003921569f, 1,1,1,0.02352941f, 1,1,1,0.1019608f, 1,1,1,0.2901961f, 1,1,1,0.8235294f, 1,1,1,0.945098f, 1,1,1,0.9215686f, 1,1,1,0.8431373f, 1,1,1,0.8078431f, 1,1,1,0.8666667f, 1,1,1,0.9490196f, 1,1,1,0.8823529f, 1,1,1,0.8196079f, 1,1,1,0.9215686f, 1,1,1,0.8784314f, 1,1,1,0.9215686f, 1,1,1,0.9215686f, 1,1,1,0.9019608f, 1,1,1,0.8823529f, 1,1,1,0.9058824f, 1,1,1,0.827451f, 1,1,1,0.8941177f, 1,1,1,0.827451f, 1,1,1,0.7529412f, 1,1,1,0.7254902f, 1,1,1,0.4039216f, 1,1,1,0.145098f, 1,1,1,0.09803922f, 1,1,1,0.1568628f, 1,1,1,0.05882353f, 1,1,1,0, 1,1,1,0, 
        1,1,1,0.007843138f, 1,1,1,0.03137255f, 1,1,1,0.1176471f, 1,1,1,0.4941176f, 1,1,1,0.945098f, 1,1,1,0.9333333f, 1,1,1,0.9137255f, 1,1,1,0.8901961f, 1,1,1,0.7843137f, 1,1,1,0.8078431f, 1,1,1,0.9215686f, 1,1,1,0.8117647f, 1,1,1,0.827451f, 1,1,1,0.8941177f, 1,1,1,0.8941177f, 1,1,1,0.9176471f, 1,1,1,0.9098039f, 1,1,1,0.9019608f, 1,1,1,0.8784314f, 1,1,1,0.8745098f, 1,1,1,0.8431373f, 1,1,1,0.9333333f, 1,1,1,0.7607843f, 1,1,1,0.7215686f, 1,1,1,0.8627451f, 1,1,1,0.6078432f, 1,1,1,0.1098039f, 1,1,1,0.09411765f, 1,1,1,0.07058824f, 1,1,1,0.02352941f, 1,1,1,0, 1,1,1,0, 
        1,1,1,0, 1,1,1,0.007843138f, 1,1,1,0.09803922f, 1,1,1,0.6156863f, 1,1,1,0.9058824f, 1,1,1,0.9333333f, 1,1,1,0.9294118f, 1,1,1,0.9058824f, 1,1,1,0.9176471f, 1,1,1,0.8745098f, 1,1,1,0.6941177f, 1,1,1,0.9176471f, 1,1,1,0.9137255f, 1,1,1,0.8117647f, 1,1,1,0.7921569f, 1,1,1,0.8745098f, 1,1,1,0.7882353f, 1,1,1,0.7098039f, 1,1,1,0.9176471f, 1,1,1,0.7607843f, 1,1,1,0.7607843f, 1,1,1,0.7215686f, 1,1,1,0.772549f, 1,1,1,0.7254902f, 1,1,1,0.7607843f, 1,1,1,0.7294118f, 1,1,1,0.1882353f, 1,1,1,0.04313726f, 1,1,1,0.01176471f, 1,1,1,0.007843138f, 1,1,1,0, 1,1,1,0, 
        1,1,1,0, 1,1,1,0, 1,1,1,0.0627451f, 1,1,1,0.6039216f, 1,1,1,0.9568627f, 1,1,1,0.9686275f, 1,1,1,0.9568627f, 1,1,1,0.9019608f, 1,1,1,0.9372549f, 1,1,1,0.827451f, 1,1,1,0.6588235f, 1,1,1,0.8941177f, 1,1,1,0.9137255f, 1,1,1,0.9058824f, 1,1,1,0.8f, 1,1,1,0.8509804f, 1,1,1,0.6196079f, 1,1,1,0.6039216f, 1,1,1,0.8f, 1,1,1,0.7647059f, 1,1,1,0.8588235f, 1,1,1,0.8862745f, 1,1,1,0.7372549f, 1,1,1,0.7960784f, 1,1,1,0.8117647f, 1,1,1,0.7803922f, 1,1,1,0.4235294f, 1,1,1,0.1137255f, 1,1,1,0.01960784f, 1,1,1,0, 1,1,1,0, 1,1,1,0, 
        1,1,1,0, 1,1,1,0, 1,1,1,0.03137255f, 1,1,1,0.4745098f, 1,1,1,0.6901961f, 1,1,1,0.7607843f, 1,1,1,0.8156863f, 1,1,1,0.9254902f, 1,1,1,0.9372549f, 1,1,1,0.7921569f, 1,1,1,0.9215686f, 1,1,1,0.9137255f, 1,1,1,0.8784314f, 1,1,1,0.8196079f, 1,1,1,0.6901961f, 1,1,1,0.4392157f, 1,1,1,0.5843138f, 1,1,1,0.8666667f, 1,1,1,0.8156863f, 1,1,1,0.6f, 1,1,1,0.6588235f, 1,1,1,0.7450981f, 1,1,1,0.7176471f, 1,1,1,0.8862745f, 1,1,1,0.9137255f, 1,1,1,0.7647059f, 1,1,1,0.7372549f, 1,1,1,0.2784314f, 1,1,1,0.06666667f, 1,1,1,0, 1,1,1,0, 1,1,1,0, 
        1,1,1,0, 1,1,1,0.003921569f, 1,1,1,0.01960784f, 1,1,1,0.2352941f, 1,1,1,0.3529412f, 1,1,1,0.3333333f, 1,1,1,0.6470588f, 1,1,1,0.9333333f, 1,1,1,0.9254902f, 1,1,1,0.9137255f, 1,1,1,0.9254902f, 1,1,1,0.9176471f, 1,1,1,0.8588235f, 1,1,1,0.654902f, 1,1,1,0.5803922f, 1,1,1,0.3137255f, 1,1,1,0.4901961f, 1,1,1,0.9254902f, 1,1,1,0.6862745f, 1,1,1,0.6980392f, 1,1,1,0.6039216f, 1,1,1,0.6588235f, 1,1,1,0.772549f, 1,1,1,0.8980392f, 1,1,1,0.8627451f, 1,1,1,0.8392157f, 1,1,1,0.7176471f, 1,1,1,0.3254902f, 1,1,1,0.09803922f, 1,1,1,0, 1,1,1,0, 1,1,1,0, 
        1,1,1,0, 1,1,1,0.01960784f, 1,1,1,0.04313726f, 1,1,1,0.1960784f, 1,1,1,0.3176471f, 1,1,1,0.345098f, 1,1,1,0.7607843f, 1,1,1,0.8392157f, 1,1,1,0.7019608f, 1,1,1,0.854902f, 1,1,1,0.8431373f, 1,1,1,0.9176471f, 1,1,1,0.8823529f, 1,1,1,0.6431373f, 1,1,1,0.4901961f, 1,1,1,0.2784314f, 1,1,1,0.5647059f, 1,1,1,0.8784314f, 1,1,1,0.454902f, 1,1,1,0.6901961f, 1,1,1,0.7294118f, 1,1,1,0.7019608f, 1,1,1,0.7568628f, 1,1,1,0.7333333f, 1,1,1,0.827451f, 1,1,1,0.7843137f, 1,1,1,0.5294118f, 1,1,1,0.1686275f, 1,1,1,0.07450981f, 1,1,1,0.02745098f, 1,1,1,0.007843138f, 1,1,1,0, 
        1,1,1,0, 1,1,1,0.03529412f, 1,1,1,0.1098039f, 1,1,1,0.4f, 1,1,1,0.345098f, 1,1,1,0.3176471f, 1,1,1,0.6156863f, 1,1,1,0.7215686f, 1,1,1,0.7843137f, 1,1,1,0.8666667f, 1,1,1,0.8509804f, 1,1,1,0.8901961f, 1,1,1,0.6901961f, 1,1,1,0.5450981f, 1,1,1,0.427451f, 1,1,1,0.227451f, 1,1,1,0.7490196f, 1,1,1,0.8901961f, 1,1,1,0.4627451f, 1,1,1,0.454902f, 1,1,1,0.682353f, 1,1,1,0.6392157f, 1,1,1,0.572549f, 1,1,1,0.5529412f, 1,1,1,0.7019608f, 1,1,1,0.627451f, 1,1,1,0.2745098f, 1,1,1,0.03529412f, 1,1,1,0.03137255f, 1,1,1,0.03529412f, 1,1,1,0.007843138f, 1,1,1,0, 
        1,1,1,0.01568628f, 1,1,1,0.06666667f, 1,1,1,0.2980392f, 1,1,1,0.5490196f, 1,1,1,0.1882353f, 1,1,1,0.09019608f, 1,1,1,0.2705882f, 1,1,1,0.5254902f, 1,1,1,0.6901961f, 1,1,1,0.8f, 1,1,1,0.7529412f, 1,1,1,0.8666667f, 1,1,1,0.8117647f, 1,1,1,0.4078431f, 1,1,1,0.2980392f, 1,1,1,0.3058824f, 1,1,1,0.7764706f, 1,1,1,0.9058824f, 1,1,1,0.7529412f, 1,1,1,0.6313726f, 1,1,1,0.5411765f, 1,1,1,0.8039216f, 1,1,1,0.9411765f, 1,1,1,0.9019608f, 1,1,1,0.8941177f, 1,1,1,0.4f, 1,1,1,0.0627451f, 1,1,1,0, 1,1,1,0.007843138f, 1,1,1,0.01176471f, 1,1,1,0, 1,1,1,0, 
        1,1,1,0.01960784f, 1,1,1,0.07058824f, 1,1,1,0.1372549f, 1,1,1,0.2078431f, 1,1,1,0.1176471f, 1,1,1,0.2f, 1,1,1,0.3803922f, 1,1,1,0.3490196f, 1,1,1,0.3921569f, 1,1,1,0.4431373f, 1,1,1,0.3960784f, 1,1,1,0.682353f, 1,1,1,0.772549f, 1,1,1,0.5529412f, 1,1,1,0.3843137f, 1,1,1,0.4156863f, 1,1,1,0.5803922f, 1,1,1,0.6588235f, 1,1,1,0.6862745f, 1,1,1,0.6156863f, 1,1,1,0.8313726f, 1,1,1,0.8901961f, 1,1,1,0.9215686f, 1,1,1,0.9058824f, 1,1,1,0.9333333f, 1,1,1,0.5490196f, 1,1,1,0.2078431f, 1,1,1,0.08627451f, 1,1,1,0.01176471f, 1,1,1,0, 1,1,1,0, 1,1,1,0, 
        1,1,1,0.003921569f, 1,1,1,0.01960784f, 1,1,1,0.05882353f, 1,1,1,0.04313726f, 1,1,1,0.1686275f, 1,1,1,0.4117647f, 1,1,1,0.5843138f, 1,1,1,0.3921569f, 1,1,1,0.2352941f, 1,1,1,0.06666667f, 1,1,1,0.3568628f, 1,1,1,0.5882353f, 1,1,1,0.7490196f, 1,1,1,0.7843137f, 1,1,1,0.7058824f, 1,1,1,0.6156863f, 1,1,1,0.4784314f, 1,1,1,0.5019608f, 1,1,1,0.5803922f, 1,1,1,0.3529412f, 1,1,1,0.7686275f, 1,1,1,0.8784314f, 1,1,1,0.9333333f, 1,1,1,0.9137255f, 1,1,1,0.9607843f, 1,1,1,0.7176471f, 1,1,1,0.3333333f, 1,1,1,0.3529412f, 1,1,1,0.2392157f, 1,1,1,0.03921569f, 1,1,1,0.003921569f, 1,1,1,0, 
        1,1,1,0, 1,1,1,0.003921569f, 1,1,1,0.02352941f, 1,1,1,0.04313726f, 1,1,1,0.1372549f, 1,1,1,0.4901961f, 1,1,1,0.7529412f, 1,1,1,0.5764706f, 1,1,1,0.1764706f, 1,1,1,0.01176471f, 1,1,1,0.1294118f, 1,1,1,0.4980392f, 1,1,1,0.7647059f, 1,1,1,0.8235294f, 1,1,1,0.8392157f, 1,1,1,0.8627451f, 1,1,1,0.5764706f, 1,1,1,0.6392157f, 1,1,1,0.6313726f, 1,1,1,0.3137255f, 1,1,1,0.6078432f, 1,1,1,0.9058824f, 1,1,1,0.9176471f, 1,1,1,0.6196079f, 1,1,1,0.7372549f, 1,1,1,0.8117647f, 1,1,1,0.4352941f, 1,1,1,0.5215687f, 1,1,1,0.5411765f, 1,1,1,0.1960784f, 1,1,1,0.01960784f, 1,1,1,0, 
        1,1,1,0, 1,1,1,0, 1,1,1,0.007843138f, 1,1,1,0.01960784f, 1,1,1,0.05882353f, 1,1,1,0.3372549f, 1,1,1,0.4588235f, 1,1,1,0.2f, 1,1,1,0.05490196f, 1,1,1,0.007843138f, 1,1,1,0.09019608f, 1,1,1,0.4078431f, 1,1,1,0.7372549f, 1,1,1,0.7490196f, 1,1,1,0.6352941f, 1,1,1,0.4196078f, 1,1,1,0.3764706f, 1,1,1,0.3882353f, 1,1,1,0.6352941f, 1,1,1,0.4784314f, 1,1,1,0.4117647f, 1,1,1,0.7372549f, 1,1,1,0.7960784f, 1,1,1,0.5607843f, 1,1,1,0.2941177f, 1,1,1,0.5294118f, 1,1,1,0.3137255f, 1,1,1,0.4117647f, 1,1,1,0.3254902f, 1,1,1,0.06666667f, 1,1,1,0.007843138f, 1,1,1,0, 
        1,1,1,0, 1,1,1,0, 1,1,1,0.007843138f, 1,1,1,0.007843138f, 1,1,1,0.003921569f, 1,1,1,0.1254902f, 1,1,1,0.1490196f, 1,1,1,0.02352941f, 1,1,1,0.007843138f, 1,1,1,0.1215686f, 1,1,1,0.4941176f, 1,1,1,0.7568628f, 1,1,1,0.7764706f, 1,1,1,0.7686275f, 1,1,1,0.3960784f, 1,1,1,0.02745098f, 1,1,1,0.007843138f, 1,1,1,0.1411765f, 1,1,1,0.5450981f, 1,1,1,0.6627451f, 1,1,1,0.5686275f, 1,1,1,0.6392157f, 1,1,1,0.627451f, 1,1,1,0.5960785f, 1,1,1,0.3882353f, 1,1,1,0.1803922f, 1,1,1,0.1019608f, 1,1,1,0.1176471f, 1,1,1,0.09019608f, 1,1,1,0.01568628f, 1,1,1,0, 1,1,1,0, 
        1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0.007843138f, 1,1,1,0.03137255f, 1,1,1,0.003921569f, 1,1,1,0.003921569f, 1,1,1,0.3019608f, 1,1,1,0.7019608f, 1,1,1,0.7490196f, 1,1,1,0.6627451f, 1,1,1,0.5843138f, 1,1,1,0.2078431f, 1,1,1,0, 1,1,1,0, 1,1,1,0.1098039f, 1,1,1,0.6901961f, 1,1,1,0.8705882f, 1,1,1,0.9098039f, 1,1,1,0.7176471f, 1,1,1,0.4039216f, 1,1,1,0.5607843f, 1,1,1,0.4392157f, 1,1,1,0.3960784f, 1,1,1,0.2666667f, 1,1,1,0.03137255f, 1,1,1,0.05098039f, 1,1,1,0.007843138f, 1,1,1,0, 1,1,1,0, 
        1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0.01176471f, 1,1,1,0.2196078f, 1,1,1,0.4705882f, 1,1,1,0.509804f, 1,1,1,0.2039216f, 1,1,1,0.05490196f, 1,1,1,0.01960784f, 1,1,1,0, 1,1,1,0, 1,1,1,0.09411765f, 1,1,1,0.4823529f, 1,1,1,0.5450981f, 1,1,1,0.5294118f, 1,1,1,0.2941177f, 1,1,1,0.2078431f, 1,1,1,0.4745098f, 1,1,1,0.5921569f, 1,1,1,0.5372549f, 1,1,1,0.6666667f, 1,1,1,0.2745098f, 1,1,1,0.02352941f, 1,1,1,0.003921569f, 1,1,1,0, 1,1,1,0, 
        1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0.007843138f, 1,1,1,0.02352941f, 1,1,1,0.09803922f, 1,1,1,0.08627451f, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0.01960784f, 1,1,1,0.003921569f, 1,1,1,0.03529412f, 1,1,1,0.01568628f, 1,1,1,0, 1,1,1,0.07843138f, 1,1,1,0.1803922f, 1,1,1,0.2431373f, 1,1,1,0.3921569f, 1,1,1,0.6117647f, 1,1,1,0.6313726f, 1,1,1,0.05882353f, 1,1,1,0, 1,1,1,0, 1,1,1,0, 
        1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,1,0.003921569f, 1,1,1,0.05882353f, 1,1,1,0.1372549f, 1,1,1,0.1607843f, 1,1,1,0.02745098f, 1,1,1,0, 1,1,1,0, 1,1,1,0, 
    };

    *width = 32, *height = 32;    

    *textureData = calloc(*width * *height * 4, sizeof(float));

    for (i=0; i < *width * *height * 4; ++i)
        (*textureData)[i] = data[i];
}
